#!/bin/sh
# vim: set ts=4:
#
# Creates build artifact for a standalone Luapak binary.
#
# Environment variables:
#   TARGET_ARCH : Architecture for which is the binary built (this is used in
#                 the archive's name).
#
set -eu

cd "$(dirname "$0")/.."

if [ -f dist/luapak ]; then
	. script/utils.sh
else
	. script/build-standalone
fi

einfo 'Creating build artifact'

test -f dist/luapak* || die 'dist/luapak does not exist!'

case "$HOST_OS" in
	MINGW*) target_os='windows';;
	Darwin) target_os='macos';;
	*) target_os=$(echo "$HOST_OS" | tr '[A-Z]' '[a-z]');;
esac

version=$(git_based_version)
dir_name="luapak-$version"
file_name="luapak-$version-$TARGET_ARCH-$target_os"

cd dist

case "$target_os" in
	windows)
		7z a $file_name.zip luapak.exe
		rm luapak.exe
		sha256sum $file_name.zip > $file_name.zip.sha256
	;;
	*)
		mkdir "$dir_name"
		mv luapak "$dir_name"/
		tar -czf "$file_name.tar.gz" "$dir_name"
		rm -r "$dir_name"
		sha256sum $file_name.tar.gz > $file_name.tar.gz.sha256
	;;
esac

cd ..
run ls -lh dist/
